<template>
  <div>
    <div style="width: 1150px;margin-left: auto;margin-right: auto;text-align: center;margin-top: 10px;">
      <el-input v-model="userquery.username" placeholder="管理员姓名" style="width: 200px;float: left;"></el-input>
      <el-input v-model="userquery.rolename" placeholder="角色名称" style="width: 200px;float: left;margin-left: 15px;"></el-input>
      <el-input v-model="userquery.starttime" type="date" placeholder="最早登录时间" style="width: 165px;float: left;margin-left: 15px;"></el-input>
      <el-input v-model="userquery.endtime" type="date" placeholder="最晚登录时间" style="width: 165px;float: left;margin-left: 10px;"></el-input>
      <el-button @click="chongzhi" type="infor" style="float: left;margin-left: 10px;">重置</el-button>
      <el-button @click="chaxun" type="primary" style="float: left;">查询</el-button>
      <el-button type="warning" style="float: right;">导出</el-button>
      <el-button @click="dialogVisiblexinzengyonghu=true" type="success" style="float: right;">新增用户</el-button>
    </div>

    <el-table
      :data="users"
      style="width: 1150px;margin-left: auto;margin-right: auto;text-align: center;">
      <el-table-column
        label="姓名"
        align="center"
        width="100px">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>姓名: {{ scope.row.username }}</p>
            <p>密码: {{ scope.row.password }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.username }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        label="密码"
        align="center"
        width="100px">
        <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.password }}</el-tag>
            </div>
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        label="充当角色"
        width="410px">
        <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag v-if="scope.row.roles[0]!=null" v-for="role in scope.row.roles" size="medium" style="margin-left: 2px;">{{ role }}</el-tag>
            </div>
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        label="产生日期"
        width="120px">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.gmtCreate }}</span>
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        label="更新日期"
        width="120px">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.gmtModified }}</span>
        </template>
      </el-table-column>
      <el-table-column
      align="center"
        label="上次登录时间"
        width="120px">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.gmtCreate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"
      align="center"
      width="180px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="openjueseeditdialog(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <!-- 新增用户弹出框 -->
    <el-dialog
      title="新增用户"
      :visible.sync="dialogVisiblexinzengyonghu"
      width="30%"
      :before-close="handleClose">
      <el-input v-model="useradd.username" type="text" placeholder="请输入用户名:" clearable="true"></el-input>
      <el-input v-model="useradd.phone" type="text" placeholder="请输入手机号码:" clearable="true" style="margin-top: 10px;"></el-input>
      <el-input v-model="useradd.password" type="password" placeholder="请输入密码:" clearable="true" style="margin-top: 10px;"></el-input>
      <el-input v-model="checkpassword" type="password" placeholder="请核对密码:" clearable="true" style="margin-top: 10px;"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="calanceadd()">取 消</el-button>
        <el-button type="primary" @click="addusercom">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 用户编辑框 -->
    <el-dialog
      title="用户角色管理"
      :visible.sync="dialogVisibleyonghujueseguanli"
      width="60%"
      :before-close="handleClose">
      <div>
        用户名:{{this.tempuname}}
      </div>
      <div style="margin-top: 10px;">
        <el-tag
          v-if="dynamicTags!=null"
          :key="tag"
          v-for="tag in dynamicTags"
          closable
          :disable-transitions="false"
          @close="handleClosex(tag)">
          {{tag}}
        </el-tag>
      </div>
      <div style="margin-top: 10px;">
        <el-select v-model="value" placeholder="请选择角色">
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
              >
            </el-option>
          </el-select>
          <el-button @click="addjuese" type="success" style="margin-left: 10px;">+添加角色</el-button>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="calanceadd">取 消</el-button>
        <el-button type="primary" @click="adduserrole">提 交</el-button>
      </span>
    </el-dialog>
    <div class="block" style="width: 1150px;margin-left: auto;margin-right: auto;">
        <el-pagination
        style="text-align: center;"
        background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="userquery.currentpage"
          :page-sizes="[6, 15, 30, 40]"
          :page-size="userquery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="usertotal">
        </el-pagination>
      </div>
  </div>

</template>

<script>
  import usermethod from '@/api/user/user'
  export default {
    data() {
      return {
        userquery:{
          currentpage: 1,
          endtime: null,
          limit: 6,
          rolename: null,
          starttime: null,
          username: null
        },
        users:null,
        //新增用户弹出框 加载参数
        dialogVisiblexinzengyonghu: false,
        //管理员注册用户
        useradd: {
            isDelete: 0,
            password: '',
            phone: '',
            username: ''
              },
        //密码核对
        checkpassword: '',
        // 分页参数
        usertotal: 0,
        //用户角色分配
        dialogVisibleyonghujueseguanli: false,
        //用户角色增减法
        dynamicTags: ['标签一', '标签二', '标签三'],
        inputVisible: false,
        inputValue: '',
        options: [],
        // options: [{
        //           value: '选项1',
        //           label: '黄金糕'
        //         }, {
        //           value: '选项2',
        //           label: '双皮奶',
        //           disabled: true
        //         }],
                value: '',
                tempuid: '',
                tempuname: '',

      }
    },
    created(){
      this.getusers()
    },
    methods: {
      //获取用户数据
      getusers(){
        if(this.userquery.username!=null || this.userquery.rolename!=null || this.userquery.starttime!=null || this.userquery.endtime!=null){
          usermethod.getuserpagesx(this.userquery).then(response =>{
            this.users=response.data.users
            usermethod.getusertotalcx(this.userquery).then(res =>{
              this.usertotal=res.data.userscount
            })
            console.log('测试用户：',this.users)
          })

        }else{
          usermethod.getusertotal().then(response => {
            this.usertotal=response.data.sum
          })
          usermethod.getuserpagesx(this.userquery).then(response =>{
            this.users=response.data.users
            console.log('测试用户：',this.users)
          })
        }

      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(id) {
        usermethod.logicdeleteus(id).then(response => {
          if(response.data.a==true){
            this.$message.success('删除成功')
            this.getusers()
          }else{
            this.$message.error('删除失败')
          }
        })
      },
      //dilog弹出框
      handleClose(done) {
              this.$confirm('确认关闭？')
                .then(_ => {
                  this.useradd.username=''
                  this.useradd.password=''
                  this.useradd.phone=''
                  this.checkpassword=''
                  this.dialogVisiblexinzengyonghu = false

                  this.value=''
                  this.tempuid=''
                  this.tempuname=''
                  this.dynamicTags=[]
                  this.dialogVisibleyonghujueseguanli=false
                  done();
                })
                .catch(_ => {});
            },
    addusercom(){
      if(this.useradd.username.length>14){
        this.$message.error('用户名的长度不能超过14！');
        return false;
      }
      if(this.useradd.username.length==0){
        this.$message.error('用户名不能为空！');
         return false;
      }
      if(this.useradd.password.length>14){
        this.$message.error('密码的长度不能超过14！');
         return false;
      }
      if(this.useradd.password.length==0 || this.checkpassword.length==0){
        this.$message.error('密码不能为空！');
         return false;
      }
      if(this.useradd.phone.length!=11 && this.useradd.phone[0]!='1'){
        this.$message.error('手机号的格式错误');
         return false;
      }
      if(this.useradd.password!=this.checkpassword){
        this.$message.error('密码核对错误！');
         return false;
      }
      else{
        usermethod.adduserx(this.useradd).then(response =>{
          if(response.data.a==false){
            this.$message.error('已经存在相同的用户！请输入其他用户名！')
            return false;
          }
          if(response.data.a==true){
            this.$message.success('注册成功！')
            this.useradd.username=''
            this.useradd.password=''
            this.useradd.phone=''
            this.checkpassword=''
            this.getusers()
            this.dialogVisiblexinzengyonghu = false
          }
        })

      }
    },
    //取消新增
    calanceadd(){
      this.useradd.username=''
      this.useradd.password=''
      this.useradd.phone=''
      this.checkpassword=''
      this.dialogVisiblexinzengyonghu = false
      //
      this.value=''
      this.tempuid=''
      this.tempuname=''
      this.dynamicTags=[]
      this.dialogVisibleyonghujueseguanli=false
    },
    //分页
    handleSizeChange(val) {
            this.userquery.limit=val
            this.userquery.currentpage=1
            this.getusers()

          },
          handleCurrentChange(val) {
           this.userquery.currentpage=val
           this.getusers()
          },
    //重置和查询
    chongzhi(){
      this.userquery.username=null
      this.userquery.rolename=null
      this.userquery.starttime=null
      this.userquery.endtime=null
    },
    chaxun(){
      this.userquery.currentpage=1
      this.getusers()
      console.log('测试查询参数：',this.userquery)
    },
    //用户角色编辑框
    openjueseeditdialog(id){
      console.log('测试编辑：',id)
      usermethod.getrolebyuid(id).then(response =>{
        if(response.data.map.roles==null){
          this.dynamicTags=[]
        }else{
          this.dynamicTags=response.data.map.roles
        }
        console.log('测试角色标签:',this.dynamicTags)
        console.log('用户；',response.data.map)
        this.tempuid=response.data.map.id
        this.tempuname=response.data.map.username
      })
       usermethod.getallrolename().then(response => {
         this.options=response.data.roles
       })
      this.dialogVisibleyonghujueseguanli=true
    },
    adduserrole(){
      var adduserrolef={
        id: this.tempuid,
        rolenames: this.dynamicTags
      }
      usermethod.adduserrolesx(adduserrolef).then(response => {
        if(response.data.b==true){
          this.$message.success('修改角色成功')
          this.getusers()
          this.value=''
          this.tempuid=''
          this.tempuname=''
          this.dynamicTags=[]
          this.dialogVisibleyonghujueseguanli=false
        }else{
          this.$message.error('修改角色失败')
        }
      })
      console.log('测试提交用户——角色参数:',adduserrole)

    },
    handleClosex(tag) {
            this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
          },

          showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
              this.$refs.saveTagInput.$refs.input.focus();
            });
          },

          handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
              this.dynamicTags.push(inputValue);
            }
            this.inputVisible = false;
            this.inputValue = '';
          },
      addjuese(){
        this.inputValue=this.value
        if(this.inputValue=='超级管理员'){
          this.$message.error('不能分配该角色')
          return false
        }
        if(this.dynamicTags.indexOf(this.inputValue)>=0){
          this.$message.error('已经存在该角色')
        }else{
           this.handleInputConfirm()
        }

      }
    }
  }
</script>
<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
